//
//  FeedbackRecordCell.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/6/22.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "FeedbackRecordCell.h"
#import "FeedbackReplyView.h"
#import <YBImageBrowser/YBImageBrowser.h>

@interface FeedbackRecordCell ()<YBImageBrowserDataSource>

@property (nonatomic, strong) UILabel *recordLab;
@property (nonatomic, strong) UILabel *recordCustomerLab;
@property (nonatomic, strong) UILabel *recordTimeLab;
@property (nonatomic, strong) FeedbackReplyView *replyView;

@property (nonatomic, strong) NSMutableArray<UIImageView *> *imageViews;

@end

@implementation FeedbackRecordCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.imageViews = @[].mutableCopy;
        [self setupSubViews];
    }
    return self;
}

- (void)setupSubViews
{
    self.recordLab = [[UILabel alloc] init];
    self.recordLab.textColor = kColor333333;
    self.recordLab.font = [UIFont systemFontOfSize:15];
    self.recordLab.numberOfLines = 0;
    [self addSubview:self.recordLab];
    
    self.recordCustomerLab = [[UILabel alloc] init];
    self.recordCustomerLab.textColor = kColor666666;
    self.recordCustomerLab.font = [UIFont systemFontOfSize:12];
    [self addSubview:self.recordCustomerLab];
    
    self.recordTimeLab = [[UILabel alloc] init];
    self.recordTimeLab.textColor = kColor666666;
    self.recordTimeLab.font = [UIFont systemFontOfSize:12];
    [self addSubview:self.recordTimeLab];
    
    self.replyView = [[FeedbackReplyView alloc] init];
    [self addSubview:self.replyView];
}

- (void)setEntity:(FeedbackRecordEntity *)entity
{
    _entity = entity;
    self.recordLab.text = entity.content;
    self.recordCustomerLab.text = entity.consumerName;
    self.recordTimeLab.text = entity.createTime;
    self.replyView.replyList = entity.replys;
    
    self.recordLab.frame = entity.contentFrame;
    self.recordCustomerLab.frame = entity.consumerNameFrame;
    self.recordTimeLab.frame = entity.createTimeFrame;
    self.replyView.frame = entity.replysFrame;
    
    for (UIView *subView in self.subviews) {
        if ([subView isKindOfClass:[UIImageView class]]) {
            if (subView.tag >= 10000) {
                [subView removeFromSuperview];
            }
        }
    }
    [self.imageViews removeAllObjects];
    
    NSArray *filePath = entity.filePaths;
    if (filePath.count) {
        CGFloat sideMagin = 15;
        CGFloat magin = 8;
        CGFloat w = (kScreenW - sideMagin*2 - magin*2) / 3;
        CGFloat h = 70;
        for (int i = 0; i < filePath.count; i++) {
            UIImageView *imgView = [[UIImageView alloc] init];
            imgView.frame = CGRectMake(sideMagin + (i % 3) * (w + magin), CGRectGetMaxY(self.recordLab.frame) + 15 + (i / 3) * (h + magin), w, h);
            NSString *url = [NSString stringWithFormat:@"%@%@",SERVERADDRS,filePath[i]];
            [imgView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"pic_moren"]];
            imgView.tag = 10000 + i;
            imgView.userInteractionEnabled = YES;
            [imgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImg:)]];
            [self addSubview:imgView];
            [self.imageViews addObject:imgView];
        }
    }
}

- (void)clickImg:(UITapGestureRecognizer *)tap
{
    UIImageView *imgView = (UIImageView *)tap.view;
    [self browserImage:imgView index:imgView.tag - 10000];
}

// 图片预览
- (void)browserImage:(UIImageView *)imageView index:(NSUInteger)index
{
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSource = self;
    browser.currentIndex = index;
    [browser show];
}

#pragma mark - YBImageBrowserDataSource
- (NSUInteger)yb_numberOfCellForImageBrowserView:(YBImageBrowserView *)imageBrowserView {
    return self.entity.filePaths.count;
}
- (id<YBImageBrowserCellDataProtocol>)yb_imageBrowserView:(YBImageBrowserView *)imageBrowserView dataForCellAtIndex:(NSUInteger)index {
    YBImageBrowseCellData *data = [YBImageBrowseCellData new];
    data.imageBlock = ^__kindof UIImage * _Nullable{
        return self.imageViews[index].image;
    };
    data.sourceObject = self.imageViews[index];
    return data;
}

@end
